package com.wwwarehouse.common.custom_widget.progressbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ProgressBar;
import com.wwwarehouse.common.R;

/**
 * @author : mundane
 * @time : 2017/6/16 10:15
 * @description :
 * @file : CircleProgressView.java
 */

public class CircleProgressView extends ProgressBar {
	private Paint mBgPaint;
	private Paint mProgressPaint;
	private Paint mTextPaint;
	private int mBgColor;
	private int mProgressColor;
	private int mTextColor;
	private int mRadius;
	private int mStrokeWidth;
	private RectF mRecfF;
	private final int DEFAULT_STROKE_WIDTH = 5;
	private int mDefaultRadius;
	// 默认背景颜色是浅灰色
	private final int DEFAULT_BG_COLOR = getResources().getColor(R.color.common_color_c8_d5d7dc);
	// 默认进度条颜色是蓝色
	private final int DEFAULT_PROGRESS_COLOR = getResources().getColor(R.color.common_color_c1_337cff);
	// 默认字体颜色
	private final int DEFAULT_TEXT_COLOR = getResources().getColor(R.color.common_color_c1_337cff);
	// 默认字体大小 26sp
	private int DEFAULT_TEXT_SIZE = 26;
	//	默认加载成功的颜色
	private final int DEFAULT_SUCCESS_COLOR = getResources().getColor(android.R.color.holo_green_dark);
	//	百分号的默认字体大小是12sp
	private int DEFAULT_PRECENT_TEXT_SIZE = 12;
	private final String PERCENT_TEXT = "%";
	private int mTextSize;

	private static final int PROGRESS_STYLE_NORMAL = 0;
	private static final int PROGRESS_STYLE_CUSTOM_DESC = 1;
	private static final int PROGRESS_STYLE_NO_BACKGROUND = 2;
	private int mProgressStyle = PROGRESS_STYLE_NORMAL;
	private String mCustomDesc;
	private int DEFAULT_DESC_TEXT_COLOR = getResources().getColor(R.color.common_color_c7_96999e);
	//	自定义描述的字体大小默认是12sp
	private final int DEFAULT_DESC_TEXT_SIZE = 12;
	private Paint mDescPaint;

	public CircleProgressView(Context context) {
		this(context, null);
	}

	public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initAttrs(context, attrs);
		initData();
	}

	private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
		mBgColor = a.getColor(R.styleable.CircleProgressView_circle_bg_color, DEFAULT_BG_COLOR);
		mProgressColor = a.getColor(R.styleable.CircleProgressView_circle_progress_color, DEFAULT_PROGRESS_COLOR);
		mTextSize = a.getDimensionPixelSize(R.styleable.CircleProgressView_text_size, dp2px(DEFAULT_TEXT_SIZE));
		mTextColor = a.getColor(R.styleable.CircleProgressView_text_color, DEFAULT_TEXT_COLOR);
		mProgressStyle = a.getInt(R.styleable.CircleProgressView_progressStyle, PROGRESS_STYLE_NORMAL);
		mCustomDesc = a.getString(R.styleable.CircleProgressView_desc);
		a.recycle();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mDefaultRadius = Math.min(getMeasuredHeight(), getMeasuredWidth()) / 2 - mStrokeWidth / 2;
	}


	@Override
	protected void onDraw(Canvas canvas) {
		int halfWidth = getMeasuredWidth() / 2;
		int halfHeight = getMeasuredHeight() / 2;
		if (mRadius == 0 || mRadius > mDefaultRadius) {
			mRadius = mDefaultRadius;
		}
		if (mRecfF == null) {
			mRecfF = new RectF(halfWidth - mRadius, halfHeight - mRadius, halfWidth + mRadius, halfHeight + mRadius);
		}
		switch (mProgressStyle) {
			case PROGRESS_STYLE_NORMAL:
				drawNormal(canvas, halfWidth, halfHeight);
				break;
			case PROGRESS_STYLE_CUSTOM_DESC:
				drawCustomDesc(canvas, halfWidth, halfHeight);
				break;
			case PROGRESS_STYLE_NO_BACKGROUND:
				drawNoBackground(canvas, halfWidth, halfHeight);
				break;
		}

	}

	private void setDesc(String desc) {
		mCustomDesc = desc;
	}

	private void drawNoBackground(Canvas canvas, int halfWidth, int halfHeight) {

		float sweepAngle = getProgress() * 1.0f / getMax() * 360;
		canvas.drawArc(mRecfF, -90, sweepAngle, false, mProgressPaint);
	}

	private void drawCustomDesc(Canvas canvas, int halfWidth, int halfHeight) {
		canvas.drawArc(mRecfF, -90, 360, false, mBgPaint);

		float sweepAngle = getProgress() * 1.0f / getMax() * 360;
		canvas.drawArc(mRecfF, -90, sweepAngle, false, mProgressPaint);

		int progress = (int) (getProgress() * 1.0f / getMax() * 100);
		String processText = String.valueOf(progress) + PERCENT_TEXT;
		mTextPaint.setTextSize(dp2px(DEFAULT_TEXT_SIZE));
		float textWidth = mTextPaint.measureText(processText);
		float textHeight = mTextPaint.descent() + mTextPaint.ascent();
//		canvas.drawText(processText, halfWidth-textWidth / 2, halfHeight, mTextPaint);

		if (mCustomDesc == null) {
			return;
		}
		float descTextWidth = mDescPaint.measureText(mCustomDesc);
		float descTextHeight = mDescPaint.descent() + mDescPaint.ascent();
//		canvas.drawText(mCustomDesc, halfWidth - descTextWidth/2, halfHeight-descTextHeight/2*3, mDescPaint);
	}

	private void drawNormal(Canvas canvas, int halfWidth, int halfHeight) {
		canvas.drawArc(mRecfF, -90, 360, false, mBgPaint);

		float sweepAngle = getProgress() * 1.0f / getMax() * 360;
		canvas.drawArc(mRecfF, -90, sweepAngle, false, mProgressPaint);

		int progress = (int) (getProgress() * 1.0f / getMax() * 100);
		String processText = String.valueOf(progress);
		mTextPaint.setTextSize(dp2px(DEFAULT_TEXT_SIZE));
		float textWidth = mTextPaint.measureText(processText);
		float textHeight = mTextPaint.descent() + mTextPaint.ascent();
//		canvas.drawText(processText, halfWidth-textWidth / 2, halfHeight-textHeight / 2, mTextPaint);

		mTextPaint.setTextSize(dp2px(DEFAULT_PRECENT_TEXT_SIZE));
		float percentTextWidth = mTextPaint.measureText(PERCENT_TEXT);
		float percentTextHeight = mTextPaint.descent() + mTextPaint.ascent();
//		canvas.drawText(PERCENT_TEXT, halfWidth+textWidth / 2, halfHeight-textHeight / 2, mTextPaint);
	}

	private void initData() {
		mStrokeWidth = dp2px(DEFAULT_STROKE_WIDTH);
		mBgPaint = createPaint(mBgColor, Paint.Style.STROKE, mStrokeWidth);
		mTextPaint = createTextPaint(mTextColor, mTextSize);
		mDescPaint = createTextPaint(DEFAULT_DESC_TEXT_COLOR, dp2px(DEFAULT_DESC_TEXT_SIZE));
		switch (mProgressStyle) {
			case PROGRESS_STYLE_NO_BACKGROUND:
				mProgressPaint = createNotRoundPaint(mProgressColor, Paint.Style.STROKE, mStrokeWidth);
				break;
			case PROGRESS_STYLE_CUSTOM_DESC:
				mProgressPaint = createPaint(mProgressColor, Paint.Style.STROKE, mStrokeWidth);

				break;
			case PROGRESS_STYLE_NORMAL:
				mProgressPaint = createPaint(mProgressColor, Paint.Style.STROKE, mStrokeWidth);
				break;
		}

	}

	private Paint createNotRoundPaint(int paintColor, Paint.Style style, int width) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		paint.setColor(paintColor);
		paint.setStyle(style);
		paint.setStrokeWidth(width);
		return paint;
	}

	private Paint createTextPaint(int paintColor, int textSize) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		paint.setColor(paintColor);
		paint.setTextSize(textSize);
		return paint;
	}

	private Paint createDescTextPaint(int paintColor, int textSize) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		paint.setColor(paintColor);
		paint.setTextSize(textSize);
		return paint;
	}

	public void setText(String text) {
		postInvalidate();
	}

	public void setStrokeWidth(int width) {
		mStrokeWidth = width;
		mBgPaint.setStrokeWidth(width);
		mProgressPaint.setStrokeWidth(width);
		postInvalidate();
	}

	public void setTextSize(int size) {
		DEFAULT_TEXT_SIZE = size;
		postInvalidate();
	}

	public void setPercentTextSize(int size) {
		DEFAULT_PRECENT_TEXT_SIZE = size;
		postInvalidate();
	}

	public void setRadius(int radius) {
		if (radius == 0 || radius > mDefaultRadius) {
			return;
		}
		postInvalidate();
	}

	private Paint createPaint(int paintColor, Paint.Style style, int width) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		paint.setColor(paintColor);
		paint.setStyle(style);
		paint.setStrokeWidth(width);
		//	这行代码会使得进度边缘变圆
		paint.setStrokeCap(Paint.Cap.ROUND);
		return paint;
	}

	public int px2dp(float paValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (paValue / scale + 0.5f);
	}

	public int dp2px(float dpValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}
